//
//  Copyright 2011 Andrey Tarantsov. Distributed under the MIT license.
//

#import <QuartzCore/QuartzCore.h>  // for self.layer.smt

#import "DemoItemView.h"


@implementation DemoItemView
@synthesize mButton, mDelegate, mFileNameFormat;


- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
//        self.layer.cornerRadius = 8;
        //self.layer.borderColor = [[UIColor grayColor] CGColor];
        //self.layer.borderWidth = 2;
        self.layer.backgroundColor = [[UIColor clearColor] CGColor];
        self.opaque = NO;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    
    mButton = [UIButton buttonWithType:UIButtonTypeCustom];

    mButton.frame = CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.height);
    [mButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:mFileNameFormat,self.tag]] forState:UIControlStateNormal];
    mButton.tag = self.tag;
    // mFileNameFormat
    // @"Avatar-%db.png"
    [mButton addTarget:self 
                action:@selector(aMethod:)
      forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:mButton];

}

- (void) aMethod:(UIButton *)pButton
{
    NSLog(@"imageTapped:%d", pButton.tag);
    [mDelegate demoItemView:self index:pButton.tag];
    
    //    UIAlertView *iAlert = [[UIAlertView alloc] initWithTitle:@"d" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    //    [iAlert show];
    //    [iAlert release];
}

@end
